package com.sunday.pinxinghui.widgets;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.aigestudio.wheelpicker.WheelPicker;
import com.sunday.common.base.BaseActivity;
import com.sunday.common.model.ResultDO;
import com.sunday.common.utils.DeviceUtils;
import com.sunday.pinxinghui.R;
import com.sunday.pinxinghui.entity.Province;
import com.sunday.pinxinghui.network.ApiClient;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by 刘涛 on 2017/8/22.
 */

public class SelectStreetWindow  extends PopupWindow implements View.OnClickListener {

    private Context mContext;

    boolean isCanReturn = false;
    int LoadLevel = 1;//加载数据等级  1是省 2是市 3是区

    //区ID
    Province street;
    List<Province> streetList = new ArrayList<>();
    List<String> streetNames=new ArrayList<>();
    private int fatherId;
    private View windowView;
    WheelPicker wheelStreet;
    LinearLayout llSelete;
    TextView btnConfirm;
    TextView btnCancel;

    private int appWith, appHeight;


    public SelectStreetWindow(Context context,int districtId) {
        this.mContext = context;
        appWith = DeviceUtils.getDisplay(mContext).widthPixels;
        appHeight = DeviceUtils.getDisplay(mContext).heightPixels;
        windowView = LayoutInflater.from(mContext).inflate(R.layout.select_street, null);
        this.setContentView(windowView);
        this.setOutsideTouchable(true);
        this.setFocusable(true);
        this.setWidth(appWith);
        this.setHeight(appHeight / 3);
        this.setOnDismissListener(new PoponDismissListener());
        this.setBackgroundDrawable(new BitmapDrawable());
        this.setOutsideTouchable(true);
        this.setAnimationStyle(R.style.popuwindow);
        fatherId=districtId;
        wheelStreet = (WheelPicker) windowView.findViewById(R.id.wheel_street);
        btnConfirm = (TextView) windowView.findViewById(R.id.btn_confirm);
        btnCancel = (TextView) windowView.findViewById(R.id.btn_cancel);
        initView();
        loadData();
        btnConfirm.setOnClickListener(this);
        btnCancel.setOnClickListener(this);
    }

    public void setFatherId(int districtId){
        fatherId=districtId;
        loadData();
    }


    private void initView() {
        wheelStreet.setVisibility(View.INVISIBLE);
        wheelStreet.setSelectedItemPosition(0);
        //区
        wheelStreet.setSelectedItemPosition(0);
        wheelStreet.setOnWheelChangeListener(new WheelPicker.OnWheelChangeListener() {
            @Override
            public void onWheelScrolled(int i) {

            }

            @Override
            public void onWheelSelected(int i) {
                //滚动结束后选中的值
                if (streetList.size() <= i) {
                    return;
                }
                street = streetList.get(i);
                isCanReturn = true;
            }

            @Override
            public void onWheelScrollStateChanged(int i) {

            }
        });
    }

    private void loadData() {
                Call<ResultDO<List<Province>>> call2 = ApiClient.getApiAdapter().getStreet(fatherId);
                call2.enqueue(new Callback<ResultDO<List<Province>>>() {
                    @Override
                    public void onResponse(Call<ResultDO<List<Province>>> call, Response<ResultDO<List<Province>>> response) {
                        ((BaseActivity) mContext).dissMissDialog();
                        if (response.body() != null && response.body().getCode() == 0) {
                            streetList.clear();
                            streetList.addAll(response.body().getResult());
                            if (streetList == null || streetList.size() == 0) {
                                return;
                            }
                            if (response.body().getResult() != null) {
                                streetList.addAll(response.body().getResult());
                            }
                            streetNames.clear();
                            for (Province province:streetList){
                                streetNames.add(province.getName());
                            }
                            wheelStreet.setData(streetNames);
                            wheelStreet.invalidate();
                            wheelStreet.setVisibility(View.VISIBLE);
                            street=streetList.get(0);
                            isCanReturn = true;
                        } else {
                            wheelStreet.setVisibility(View.INVISIBLE);
                        }

                    }

                    @Override
                    public void onFailure(Call<ResultDO<List<Province>>> call, Throwable t) {
                        ((BaseActivity) mContext).dissMissDialog();
                    }
                });



    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_confirm:
                if (!isCanReturn) {
                    return;
                }
                if (isCanReturn && onSelectListener != null) {
                    onSelectListener.onSelect(street);
                    backgroundAlpha(1f);
                    dismiss();
                }
                break;
            case R.id.btn_cancel:
                dismiss();
                break;
        }
    }

    //显示
    public void showPopupWindow(View parent) {
        if (!this.isShowing()) {
            backgroundAlpha(0.5f);
            this.showAtLocation(parent, Gravity.BOTTOM, 0, 0);
        } else {
            this.dismiss();
        }
    }

    private OnCitySelectListener onSelectListener;

    public void setOnSelectListener(OnCitySelectListener onSelectListener) {
        this.onSelectListener = onSelectListener;
    }


    public interface OnCitySelectListener {
        void onSelect(Province street);
}


    /**
     * 弹出的popWin关闭的事件，主要是为了将背景透明度改回来
     */
    class PoponDismissListener implements PopupWindow.OnDismissListener {

        @Override
        public void onDismiss() {
            // TODO Auto-generated method stub
            //Log.v("List_noteTypeActivity:", "我是关闭事件");
            backgroundAlpha(1f);
//            dismiss();
        }
    }


    /**
     * 设置添加屏幕的背景透明度
     */
    public void backgroundAlpha(float bgAlpha) {
        WindowManager.LayoutParams lp = ((Activity) mContext).getWindow().getAttributes();
        lp.alpha = bgAlpha; //0.0-1.0
        ((Activity) mContext).getWindow().setAttributes(lp);
    }
}

